Fix excessive thread name lookups#790
Conversation
py-spy uses a cache to minimize the overhead of looking up Python thread names during sampling. If looking up a thread name fails, the entire cache is rebuilt. However, if a failure is persistent, this causes the cache to be rebuilt during every sample (potentially more than once), creating excessive overhead. Two cases when persistent thread name lookup failures can occur are: * When the `threading` module has not been imported * When a thread was created outside of the `threading` module, such as through `_thread.start_new_thread` Fix this by modifying the cache to store a negative response (`None`) if the thread name is still not found after rebuilding the cache. To prevent negative responses from being lost when more than one thread name lookup fails, add a flag preventing the cache from being rebuilt more than once per sample. One disadvantage of this approach is that in nonblocking mode, a spurious negative response can be cached for an extended duration. This happens if the thread is created during a sample and after the single allowed thread name lookup happens.
|
I traced the code path. The issue is in Two compounding problems: 1. O(N^2) per sample: 2. No negative caching: When Fix: move the lookup out of the per-thread function and into // Add to PythonSpy struct:
pub thread_name_lookup_attempted: bool,
// In _get_stack_traces, before the thread loop:
if !self.thread_name_lookup_attempted {
self.python_thread_names = thread_name_lookup(self).unwrap_or_default();
self.thread_name_lookup_attempted = true;
}
// Simplified _get_python_thread_name:
fn _get_python_thread_name(&mut self, python_thread_id: u64) -> Option<String> {
self.python_thread_names.get(&python_thread_id).cloned()
}This ensures the full dict walk happens exactly once per sample (or once ever if the threading module is absent). The stale-cache issue from recycled TIDs is already handled in |
py-spy uses a cache to minimize the overhead of looking up Python thread names during sampling. If looking up a thread name fails, the entire cache is rebuilt. However, if a failure is persistent, this causes the cache to be rebuilt during every sample (potentially more than once), creating excessive overhead.
Two cases when persistent thread name lookup failures can occur are:
threadingmodule has not been importedthreadingmodule, such as through_thread.start_new_threadFix this by modifying the cache to store a negative response (
None) if the thread name is still not found after rebuilding the cache. To prevent negative responses from being lost when more than one thread name lookup fails, add a flag preventing the cache from being rebuilt more than once per sample.One disadvantage of this approach is that in nonblocking mode, a spurious negative response can be cached for an extended duration. This happens if the thread is created during a sample and after the single allowed thread name lookup happens.